package com.example.growthmuseum.goodsdesc;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.growthmuseum.R;
import com.example.growthmuseum.ServiceFactory;
import com.example.growthmuseum.data.model.Goods;
import com.example.growthmuseum.data.model.Result;
import com.example.growthmuseum.flow.FlowActivity;
import com.example.growthmuseum.service.GoodsService;
import com.orhanobut.logger.Logger;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Roman on 2016/10/29.
 */
public class GoodsDescActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView goodsInfoText;

    private TextView goodsNameText;

    private ImageView goodsPicImg;

    private Button goodsStatusBtn;

    private Button donateBtn;

    private Button fetchBtn;

    private GoodsService goodsService;

    private Goods goods;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_desc);

        setUpToolbar();
        initViews();

        // 获取物品详情
        getGoodsDesc();
    }

    private void setUpToolbar() {
        // Set up the toolbar.

//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//        ActionBar actionBar = getSupportActionBar();
//        actionBar.setDisplayHomeAsUpEnabled(true);
//        actionBar.setDisplayShowHomeEnabled(true);
    }

    private void initViews() {
        goodsInfoText = (TextView) findViewById(R.id.goods_info_text);
        goodsPicImg = (ImageView) findViewById(R.id.goods_pic_img);
        goodsNameText = (TextView) findViewById(R.id.goods_name_text);
        goodsStatusBtn = (Button) findViewById(R.id.goods_status_btn);
        goodsStatusBtn.setOnClickListener(this);
        donateBtn = (Button) findViewById(R.id.donate_btn);
        donateBtn.setOnClickListener(this);
        fetchBtn = (Button) findViewById(R.id.fetch_btn);
        fetchBtn.setOnClickListener(this);
    }

    public void getGoodsDesc() {
        Integer id = this.getIntent().getIntExtra("id", 0);
        this.goodsService = ServiceFactory
                .createRetrofitService(GoodsService.class,
                        ServiceFactory.BASE_URL);
        goodsService.getGoodsDesc(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Result<Goods>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e(e, e.getMessage());

                    }

                    @Override
                    public void onNext(Result<Goods> result) {
                        if (result.getCode().equals(Result.SUCCESS)) {
                            setGoods(result.getBean());
                        } else {
                            Toast.makeText(GoodsDescActivity.this,
                                    result.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void setGoods(Goods goods) {
        this.goods = goods;
        Glide.with(this)
                .load(goods.getGoodsPic())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(goodsPicImg);
        goodsNameText.setText(goods.getGoodsName());
        goodsInfoText.setText(goods.getGoodsInfo());
        // 如果是已收到
        if (goods.getGoodsStatus().equals(2)) {
            goodsStatusBtn.setText(getResources().getString(R.string.have_received));
            goodsStatusBtn.setClickable(false);
            if (goods.getDonate().equals(1)) {
                donateBtn.setText(getResources().getString(R.string.have_donate));
            }
            fetchBtn.setVisibility(View.VISIBLE);
            donateBtn.setVisibility(View.VISIBLE);
        } else {
            goodsStatusBtn.setText(getResources().getString(R.string.on_road));
            fetchBtn.setVisibility(View.GONE);
            donateBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goods_status_btn:
                if (!goodsStatusBtn.getText().equals(getResources().getString(R.string.have_received))) {

                    Intent toGlow = new Intent(GoodsDescActivity.this, FlowActivity.class);
                    startActivity(toGlow);
                }
                break;
            case R.id.donate_btn:
                if (donateBtn.getText().equals(getResources().getString(R.string.have_donate))) {
                    Toast.makeText(GoodsDescActivity.this, getResources().getString(R.string.have_donate), Toast.LENGTH_SHORT).show();
                } else {
                    AlertDialog.Builder builder =
                            new android.support.v7.app.AlertDialog.Builder(GoodsDescActivity.this);
                    builder.setTitle(getResources().getString(R.string.attention));
                    builder.setMessage(getResources().getString(R.string.donate));
                    builder.setNegativeButton(getResources().getString(R.string.cancel), null);
                    builder.setPositiveButton(getResources().getString(R.string.ok), (dialog1, which) -> {
                        goods.setDonate(1);
                        goods.setId(GoodsDescActivity.this.getIntent().getIntExtra("id", 0));
                        goodsService.updateGoods(goods)
                                .subscribeOn(Schedulers.newThread())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Subscriber<Result<Integer>>() {
                                    @Override
                                    public void onCompleted() {

                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        Logger.e(e, e.getMessage());

                                    }

                                    @Override
                                    public void onNext(Result<Integer> result) {
                                        if (result.getCode().equals(Result.SUCCESS)) {
                                            donateBtn.setText(getResources().getString(R.string.have_donate));
                                        } else {
                                            Toast.makeText(GoodsDescActivity.this,
                                                    result.getMessage(), Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                    });
                    builder.show();
                }
                break;
            case R.id.fetch_btn:
                AlertDialog.Builder builder =
                        new android.support.v7.app.AlertDialog.Builder(GoodsDescActivity.this);
                builder.setTitle(getResources().getString(R.string.attention));
                builder.setMessage(getResources().getString(R.string.fetch));
                builder.setNegativeButton(getResources().getString(R.string.cancel), null);
                builder.setPositiveButton(getResources().getString(R.string.ok), null);
                builder.show();
                break;
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}
